public without sharing class EventHandler {
    
    public static void eventCountAndupdate(List<Event> list_new ,Map<Id,Event> oldMap){

    	//1.去掉最近活动
		//2.统计到业务机会上
		Set<Id>  oppset = new Set<Id>();
		Set<Id> eset = new Set<Id>();
		for(Event e : list_new ){
			Event olde = oldMap.get(e.Id) ;
			if(e.Opportunity__c != null && e.ArriveTime__c != null && olde.ArriveTime__c == null   && e.LastVisit__c == true && olde.LastVisit__c != true  ){
				eset.add(e.Id);
			}
			if(e.Opportunity__c != null )oppset.add(e.Opportunity__c);
		}

		System.debug('EventHandler==>'+eset);
		System.debug('EventHandler==>'+oppset);

		if(eset.size() == 0) return;
		List<Event> eList = new List<Event>();
		Map<Id,Integer> oppEventCount = new Map<Id,Integer>();
		for(Event e :[select Id ,ArriveTime__c,LastVisit__c ,Opportunity__c  
			from Event where Opportunity__c in: oppset  and Id not in:eset  
			and LastVisit__c  = true  and RecordType.DeveloperName = 'Visit'   ]){
				e.LastVisit__c = false;
				eList.add(e);
			
		}
		oppset.clear();
		eset.clear();

		if(eList.size() > 0 ) update eList;
		
    }


    public static void validateCompelete(List<Event>list_new ,Map<Id,Event> oldMap){

    	Map<String,Event> eMap = new Map<String ,Event>();
        Map<String,String> oeMap = new Map<String,String>();
    	for (Event e  : list_new ) {
    		Event old = oldMap != null ? oldMap.get(e.Id) : new Event() ;
            if(old == null ) old =new Event();
          
    		if(e.Opportunity__c != null && e.Status__c ==  '完成' && old.Status__c != e.Status__c   ){

    			eMap.put(e.Id, e );
                oeMap.put(e.Opportunity__c, e.Id);
                // if(oldMap == null ) {//新建的时候 如果状态为完成 直接报错
                //     e.addError('没有填写意向产品跟进不能完成!');
                //     return;
                // }

    		}
    	}
    	if(eMap.isEmpty()) return;
        Map<String,Event> enMap = new Map<String ,Event>();
        for (IntentProduct__c ip  :[select Id ,Opportunity__c from IntentProduct__c where Opportunity__c in:oeMap.keySet() and Product__r.Category0__c = '仪器'  ] ) {
            if(oeMap.containsKey(ip.Opportunity__c)){
                String eId  = oeMap.get(ip.Opportunity__c);
                enMap.put(eId, eMap.get(eId));
            }
        }
        if(enMap.isEmpty()) return;
        for(IntentionalProductMonthFollow__c ip : [select Id ,ActivityID__c from IntentionalProductMonthFollow__c  where ActivityID__c in:enMap.keySet()] ){

           if( ip.ActivityID__c != null && enMap.containsKey(ip.ActivityID__c))enMap.remove(ip.ActivityID__c);
        }
        for(Event e :enMap.values()){

            e.addError('没有填写意向产品跟进不能完成!');
             return;
        }

    	// for (IntentProduct__c ip :[select Id, Opportunity__c  , (select Id, ActivityID__c from IntentProductt67M__r )  
    	// 	from IntentProduct__c where Product__r.Category0__c = '仪器' and Opportunity__c in:eMap.keySet()] ) {
    	// 	if(ip.IntentProductt67M__r.size() == 0 ){
    	// 		Event e = eMap.get(ip.Opportunity__c);
    	// 		e.addError('没有填写意向产品的月度跟进不能完成!');
    	// 		return;
    	// 	}
    	// }
    	//查找仪器的意向产品 是否有意向产品月度跟进


    }

    public static void forbidDelete (List<Event> eList){

    	Set<Id>  wpset  = new Set<Id>();
    	for(Event e : eList){
    		if(e.Week_Plan__c != null )wpset.add(e.Week_Plan__c);
    	}
    	if(wpset.size() == 0 ) return;
    	Set<Id>  wekSet = new Set<Id>();
    	for(Weekly_Plan__c w :  [select Id  from Weekly_Plan__c where Id in: wpset and Status__c= '已批准']){
    		wekSet.add(w.Id);
    	}
    	if(wekSet.size() == 0) return;
    	for(Event e : eList){
    		if(wekSet.contains(e.Week_Plan__c)){
    			e.addError('关联的周计划已批准,不能删除!');
    			return;
    		}
    	}



    }
    public static void eventCount(List<Event> eList){
		Set<Id>  oppset = new Set<Id>();

    	for(Event e : eList){
			if(e.Opportunity__c  != null )oppset.add(e.Opportunity__c);
    	}

    	if(oppset.size() == 0)return;
		Map<Id,Integer> oppEventCount = new Map<Id,Integer>();

    	for(Event e :[select Id ,ArriveTime__c,LastVisit__c ,Opportunity__c  from Event where Opportunity__c =: oppset and RecordType.DeveloperName = 'Visit' ]){
			
			if(e.ArriveTime__c != null )oppEventCount.put(e.Opportunity__c,oppEventCount.containsKey(e.Opportunity__c) ? oppEventCount.get(e.Opportunity__c) + 1 : 1) ;
		}

		if(oppEventCount.keySet().size() == 0) return;

		List<Opportunity> oppList = new List<Opportunity>();

		for(Id oppId : oppEventCount.keySet()){

			oppList.add(new Opportunity(Id = oppId ,VisitFrequency__c = oppEventCount.get(oppId)));

		}
		update oppList;


    }
}